package com.albaba.designpattern.beans.xml;

import com.albaba.designpattern.beans.BeanDefinition;
import com.albaba.designpattern.beans.MutablePropertyValues;
import com.albaba.designpattern.beans.PropertyValue;
import com.albaba.designpattern.beans.support.BeanDefinitionReader;
import com.albaba.designpattern.beans.support.BeanDefinitionRegistry;
import com.albaba.designpattern.beans.support.SimpleBeanDefinitionRegistry;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * @author zx
 * @version 1.0
 * @description 该类是对 XML 文件进行解析的具体类
 * @date 2025/3/8 19:48
 */
public class XmlBeanDefinitionReader implements BeanDefinitionReader {

    // 声明注册表对象
    private BeanDefinitionRegistry registry;

    public XmlBeanDefinitionReader() {
        this.registry = new SimpleBeanDefinitionRegistry();
    }

    @Override
    public BeanDefinitionRegistry getRegistry() {
        return registry;
    }

    @Override
    public void loadBeanDefinitions(String configLocation) throws Exception {

        // 使用 Dom4j 进行解析 XML
        SAXReader saxReader = new SAXReader();

        // 获取指向配置文件的输入流
        InputStream inputStream = XmlBeanDefinitionReader.class.getClassLoader().getResourceAsStream(configLocation);

        // 文档对象
        Document document = saxReader.read(inputStream);

        // 获取根标签
        Element rootElement = document.getRootElement();

        // 解析 Bean 标签
        pareBean(rootElement);


    }

    private void pareBean(Element rootElement) {
        // 获取所有的 Bean 标签
        List<Element> elements = rootElement.elements();

        // 遍历每个 Bean 标签，获取id className 属性以及 property 子标签
        for (Element element : elements) {
            String id = element.attributeValue("id");
            String className = element.attributeValue("class");

            // 封装数据到BeanDefinition对象中
            BeanDefinition beanDefinition = new BeanDefinition();
            beanDefinition.setId(id);
            beanDefinition.setClassName(className);

            // 获取 property 子标签
            List<Element> elementList = element.elements("property");

            MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();

            for (Element property : elementList) {
                String name = property.attributeValue("name");
                String value = property.attributeValue("value");
                String ref = property.attributeValue("ref");
                PropertyValue propertyValue = new PropertyValue(name, ref, value);

                mutablePropertyValues.addPropertyValue(propertyValue);
            }

            // 将 mutablePropertyValues 封装到 BeanDefinition 中
            beanDefinition.setPropertyValues(mutablePropertyValues);

            System.out.println(beanDefinition);

            // 将 beanDefinition 添加到注册表中
            registry.registerBeanDefinition(id, beanDefinition);

        }

    }

    public static void main(String[] args) throws Exception {
        XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader();

        xmlBeanDefinitionReader.loadBeanDefinitions("bean.xml");
    }
}
